Package aspect.editor

Source Code of aspect.editor.EditorBlock

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aspect.editor;

import static aspect.core.AspectRenderer.draw3D;
import aspect.entity.Entity;
import aspect.render.Material;
import aspect.render.ViewModel;
import aspect.render.shader.ShaderProgram;
import static aspect.resources.Resources.*;
import aspect.util.Color;
import aspect.util.Vector3;
import java.util.HashMap;

/**
*
* @author MillerV
*/
public class EditorBlock extends Entity implements Drawable2D {

    private final ShaderProgram shader;

    public Vector3 min;
    public Vector3 max;

    private ViewModel model;
    private final Color color;

    public Material material;

    private static final HashMap<Color, EditorBlock> blocks = new HashMap<>();

    public boolean valid = false;

    public static boolean selectionMode = false;

    public EditorBlock(ShaderProgram shader, Material material, Vector3 min, Vector3 max) {
        this.shader = shader;
        this.material = material;
        this.min = min;
        this.max = max;

        Color c;
        do {
            c = Color.random(false);
        } while (blocks.containsKey(c));

        this.color = c;
        revalidate();
        blocks.put(c, this);
    }

    public static EditorBlock select(Color c) {
        return blocks.get(c);
    }

    @Override
    public void render() {
        revalidate();
        if (selectionMode) {
            shader.setVar("color", color);
            shader.bind();
        } else {
            shader.unbind();
        }
        draw3D(model, pos, ang);
        shader.unbind();
    }

    public void revalidate() {
        if (!valid) {
            if (min.x > max.x) {
                float t = min.x;
                min.x = max.x;
                max.x = t;
            }
           
            if (min.y > max.y) {
                float t = min.y;
                min.y = max.y;
                max.y = t;
            }
           
            if (min.z > max.z) {
                float t = min.z;
                min.z = max.z;
                max.z = t;
            }
           
            pos = position();
            Vector3 size = size();
            model = box(material, size.x, size.y, size.z, size.x, size.y, size.z);
            valid = true;
        }
    }

    public Vector3 position() {
        return min.plus(max).times(0.5f);
    }

    public Vector3 size() {
        return max.minus(min);
    }
   
    @Override
    public Vector3 max() {
        return max;
    }
   
    @Override
    public Vector3 min() {
        return min;
    }

    @Override
    public Color color() {
        return color;
    }
}
TOP

Related Classes of aspect.editor.EditorBlock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.